﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Deflection
{
    class Particle
    {

        public Vector2 Position { get; set; }
        public float Rotation { get; set; }
        public float Scale { get; set; }
        public float Speed { get; set; }

        public Vector2 Direction { get; set; }


        public Color Color;

        public int Life { get; set; }
        public int MaxLife { get; set; }

        public bool Alive { get; set; }
        public float FadeRate { get; set; }

        public Texture2D Texture { get; set; }

        public bool Fade { get; set; }

        public Vector2 Velocity { get; set; }

        public Particle(Vector2 position, Color color, Vector2 velocity, Texture2D texture, float scale, int maxLife, float fadeRate)
        {
            Position = position;
            Color = color;
            //Direction = direction;
            Texture = texture;
            Scale = scale;
            FadeRate = fadeRate;
            Life = 0;
            MaxLife = maxLife; // 60 updates = 1 second

            Fade = true;
            Velocity = velocity;
        }

        public void Update(GameTime gameTime)
        {
            Life++;
            //Position = Vector2.Add(Position, Direction);
            // Fade out the alhpa value
            if (Fade)
            {
                //Color.A = (byte)MathHelper.Lerp((float)Color.A, 40, 0.025f);
                Color = Color * FadeRate;
                if (Life > MaxLife * 3/4 && FadeRate == 1.0f)
                {
                    FadeRate = 0.9f;
                }
            }

            Position += Velocity;


        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(Texture, Position, null, Color, Rotation, new Vector2(Texture.Width / 2, Texture.Height / 2), Scale, SpriteEffects.None, /*0.9f*/ 1);
        }
    }
}
